package bb.lanxing.util.map;

import android.location.Location;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.CoordinateConverter;

import bb.lanxing.model.map.GeoPoint;
import bb.lanxing.lib.kt.model.geo.IGeoPoint;
import bb.lanxing.util.LocCountry;

import java.util.ArrayList;
import java.util.List;

public class GeoCoorConverter {
    static double a = 6378245.0d;
    private static final CoordinateConverter converter = new CoordinateConverter();
    static double ee = 0.006693421622965943d;
    private static double x_pi = 52.35987755982988d;

    public static IGeoPoint baidu2Earth(IGeoPoint baidu) {
        return !LocCountry.inChina(baidu, true) ? baidu : common2Earth(baidu2Common(baidu));
    }

    public static IGeoPoint baidu2Common(IGeoPoint baidu) {
        if (LocCountry.inChina(baidu, true)) {
            double longitude = baidu.getLongitude() - 0.0065d;
            double latitude = baidu.getLatitude() - 0.006d;
            double sqrt = Math.sqrt((longitude * longitude) + (latitude * latitude)) - (Math.sin(x_pi * latitude) * 2.0E-5d);
            double atan2 = Math.atan2(latitude, longitude) - (Math.cos(longitude * x_pi) * 3.0E-6d);
            GeoPoint fromCommon = GeoPoint.fromCommon(sqrt * Math.sin(atan2), Math.cos(atan2) * sqrt);
            return LocCountry.inTW_HK(baidu) ? common2EarthDirectly(fromCommon) : fromCommon;
        }
        return baidu;
    }

    public static IGeoPoint earth2Baidu(IGeoPoint earth) {
        if (LocCountry.inChina(earth, true)) {
            CoordinateConverter coordinateConverter = converter;
            coordinateConverter.coord(new LatLng(earth.getLatitude(), earth.getLongitude()));
            coordinateConverter.from(CoordinateConverter.CoordType.GPS);
            LatLng convert = coordinateConverter.convert();
            return GeoPoint.fromBaidu(convert.latitude, convert.longitude);
        }
        return earth;
    }

    public static IGeoPoint common2Baidu(IGeoPoint common) {
        if (LocCountry.inChina(common, true)) {
            CoordinateConverter coordinateConverter = converter;
            coordinateConverter.coord(new LatLng(common.getLatitude(), common.getLongitude()));
            if (LocCountry.inTW_HK(common)) {
                coordinateConverter.from(CoordinateConverter.CoordType.GPS);
            } else {
                coordinateConverter.from(CoordinateConverter.CoordType.COMMON);
            }
            LatLng convert = coordinateConverter.convert();
            return GeoPoint.fromBaidu(convert.latitude, convert.longitude);
        }
        return common;
    }

    public static Location earth2Baidu(Location location) {
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        CoordinateConverter coordinateConverter = new CoordinateConverter();
        coordinateConverter.coord(latLng);
        coordinateConverter.from(CoordinateConverter.CoordType.GPS);
        LatLng convert = coordinateConverter.convert();
        location.setLatitude(convert.latitude);
        location.setLongitude(convert.longitude);
        return location;
    }

    public static IGeoPoint common2Earth(IGeoPoint common) {
        return !LocCountry.inChina(common, false) ? common : common2EarthDirectly(common);
    }

    public static IGeoPoint common2EarthDirectly(IGeoPoint common) {
        double latitude = common.getLatitude();
        double longitude = common.getLongitude();
        double d = longitude - 105.0d;
        double d2 = latitude - 35.0d;
        double transformLat = transformLat(d, d2);
        double transformLon = transformLon(d, d2);
        double d3 = (latitude / 180.0d) * 3.141592653589793d;
        double sin = Math.sin(d3);
        double d4 = 1.0d - ((ee * sin) * sin);
        double sqrt = Math.sqrt(d4);
        double d5 = a;
        return GeoPoint.fromEarth(latitude - ((transformLat * 180.0d) / ((((1.0d - ee) * d5) / (d4 * sqrt)) * 3.141592653589793d)), longitude - ((transformLon * 180.0d) / (((d5 / sqrt) * Math.cos(d3)) * 3.141592653589793d)));
    }

    private static IGeoPoint earth2CommonDirectly(IGeoPoint earth) {
        double latitude = earth.getLatitude();
        double longitude = earth.getLongitude();
        double d = longitude - 105.0d;
        double d2 = latitude - 35.0d;
        double transformLat = transformLat(d, d2);
        double transformLon = transformLon(d, d2);
        double d3 = (latitude / 180.0d) * 3.141592653589793d;
        double sin = Math.sin(d3);
        double d4 = 1.0d - ((ee * sin) * sin);
        double sqrt = Math.sqrt(d4);
        double d5 = a;
        return GeoPoint.fromCommon(latitude + ((transformLat * 180.0d) / ((((1.0d - ee) * d5) / (d4 * sqrt)) * 3.141592653589793d)), longitude + ((transformLon * 180.0d) / (((d5 / sqrt) * Math.cos(d3)) * 3.141592653589793d)));
    }

    public static IGeoPoint earth2Common(IGeoPoint latLng) {
        return LocCountry.inChina(latLng, false) ? earth2CommonDirectly(latLng) : latLng;
    }

    public static List<IGeoPoint> earth2Common(List<IGeoPoint> latLngList) {
        if (LocCountry.inChina(latLngList.get(latLngList.size() / 2), false)) {
            ArrayList arrayList = new ArrayList();
            for (IGeoPoint iGeoPoint : latLngList) {
                arrayList.add(earth2CommonDirectly(iGeoPoint));
            }
            return arrayList;
        }
        return latLngList;
    }

    static double transformLat(double x, double y) {
        double d = x * 2.0d;
        double d2 = y * 3.141592653589793d;
        return (-100.0d) + d + (y * 3.0d) + (y * 0.2d * y) + (0.1d * x * y) + (Math.sqrt(Math.abs(x)) * 0.2d) + ((((Math.sin((6.0d * x) * 3.141592653589793d) * 20.0d) + (Math.sin(d * 3.141592653589793d) * 20.0d)) * 2.0d) / 3.0d) + ((((Math.sin(d2) * 20.0d) + (Math.sin((y / 3.0d) * 3.141592653589793d) * 40.0d)) * 2.0d) / 3.0d) + ((((Math.sin((y / 12.0d) * 3.141592653589793d) * 160.0d) + (Math.sin(d2 / 30.0d) * 320.0d)) * 2.0d) / 3.0d);
    }

    static double transformLon(double x, double y) {
        double d = x * 0.1d;
        return x + 300.0d + (y * 2.0d) + (d * x) + (d * y) + (Math.sqrt(Math.abs(x)) * 0.1d) + ((((Math.sin((6.0d * x) * 3.141592653589793d) * 20.0d) + (Math.sin((x * 2.0d) * 3.141592653589793d) * 20.0d)) * 2.0d) / 3.0d) + ((((Math.sin(x * 3.141592653589793d) * 20.0d) + (Math.sin((x / 3.0d) * 3.141592653589793d) * 40.0d)) * 2.0d) / 3.0d) + ((((Math.sin((x / 12.0d) * 3.141592653589793d) * 150.0d) + (Math.sin((x / 30.0d) * 3.141592653589793d) * 300.0d)) * 2.0d) / 3.0d);
    }
}